<template>
  <el-dialog
    :title="!dataForm.repairsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="维修日期" prop="repairsTime">
      <el-input v-model="dataForm.repairsTime" placeholder="维修日期"></el-input>
    </el-form-item>
    <el-form-item label="报修人" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="报修人"></el-input>
    </el-form-item>
    <el-form-item label="报修人手机号" prop="userMobile">
      <el-input v-model="dataForm.userMobile" placeholder="报修人手机号"></el-input>
    </el-form-item>
    <el-form-item label="维修状态 1:维修 2:报废 3:完结" prop="repairsStatus">
      <el-input v-model="dataForm.repairsStatus" placeholder="维修状态 1:维修 2:报废 3:完结"></el-input>
    </el-form-item>
    <el-form-item label="资产名字" prop="assetsName">
      <el-input v-model="dataForm.assetsName" placeholder="资产名字"></el-input>
    </el-form-item>
    <el-form-item label="维修说明" prop="repairsDesc">
      <el-input v-model="dataForm.repairsDesc" placeholder="维修说明"></el-input>
    </el-form-item>
    <el-form-item label="照片附件" prop="repairsPic">
      <el-input v-model="dataForm.repairsPic" placeholder="照片附件"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          repairsId: 0,
          repairsTime: '',
          userName: '',
          userMobile: '',
          repairsStatus: '',
          assetsName: '',
          repairsDesc: '',
          repairsPic: ''
        },
        dataRule: {
          repairsTime: [
            { required: true, message: '维修日期不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '报修人不能为空', trigger: 'blur' }
          ],
          userMobile: [
            { required: true, message: '报修人手机号不能为空', trigger: 'blur' }
          ],
          repairsStatus: [
            { required: true, message: '维修状态 1:维修 2:报废 3:完结不能为空', trigger: 'blur' }
          ],
          assetsName: [
            { required: true, message: '资产名字不能为空', trigger: 'blur' }
          ],
          repairsDesc: [
            { required: true, message: '维修说明不能为空', trigger: 'blur' }
          ],
          repairsPic: [
            { required: true, message: '照片附件不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.repairsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.repairsId) {
            this.$http({
              url: this.$http.adornUrl(`/assets/repairs/info/${this.dataForm.repairsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.repairsTime = data.repairs.repairsTime
                this.dataForm.userName = data.repairs.userName
                this.dataForm.userMobile = data.repairs.userMobile
                this.dataForm.repairsStatus = data.repairs.repairsStatus
                this.dataForm.assetsName = data.repairs.assetsName
                this.dataForm.repairsDesc = data.repairs.repairsDesc
                this.dataForm.repairsPic = data.repairs.repairsPic
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/assets/repairs/${!this.dataForm.repairsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'repairsId': this.dataForm.repairsId || undefined,
                'repairsTime': this.dataForm.repairsTime,
                'userName': this.dataForm.userName,
                'userMobile': this.dataForm.userMobile,
                'repairsStatus': this.dataForm.repairsStatus,
                'assetsName': this.dataForm.assetsName,
                'repairsDesc': this.dataForm.repairsDesc,
                'repairsPic': this.dataForm.repairsPic
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
